export const convertTemperature = (value, fromUnit, toUnit) => {
    // 先转换为摄氏度
    let celsius;
    switch (fromUnit) {
        case 'celsius':
            celsius = value;
            break;
        case 'fahrenheit':
            celsius = (value - 32) * 5 / 9;
            break;
        case 'kelvin':
            celsius = value - 273.15;
            break;
        default:
            return value;
    }

    // 从摄氏度转换为目标单位
    switch (toUnit) {
        case 'celsius':
            return celsius;
        case 'fahrenheit':
            return celsius * 9 / 5 + 32;
        case 'kelvin':
            return celsius + 273.15;
        default:
            return value;
    }
};

export const convertLength = (value, fromUnit, toUnit) => {
    // 转换为米作为中间单位
    const toMeter = {
        meter: 1,
        kilometer: 1000,
        centimeter: 0.01,
        millimeter: 0.001,
        mile: 1609.344,
        yard: 0.9144,
        foot: 0.3048,
        inch: 0.0254
    };

    const meters = value * toMeter[fromUnit];

    // 从米转换为目标单位
    const fromMeter = {
        meter: 1,
        kilometer: 1 / 1000,
        centimeter: 100,
        millimeter: 1000,
        mile: 1 / 1609.344,
        yard: 1 / 0.9144,
        foot: 1 / 0.3048,
        inch: 1 / 0.0254
    };

    return meters * fromMeter[toUnit];
};

export const convertTime = (value, fromUnit, toUnit) => {
    // 转换为秒作为中间单位
    const toSecond = {
        second: 1,
        millisecond: 0.001,
        minute: 60,
        hour: 3600,
        day: 86400,
        week: 604800,
        month: 2629800, // 近似值
        year: 31557600  // 近似值
    };

    const seconds = value * toSecond[fromUnit];

    // 从秒转换为目标单位
    const fromSecond = {
        second: 1,
        millisecond: 1000,
        minute: 1 / 60,
        hour: 1 / 3600,
        day: 1 / 86400,
        week: 1 / 604800,
        month: 1 / 2629800,
        year: 1 / 31557600
    };

    return seconds * fromSecond[toUnit];
};

export const convertWeight = (value, fromUnit, toUnit) => {
    // 转换为克作为中间单位
    const toGram = {
        gram: 1,
        kilogram: 1000,
        milligram: 0.001,
        ton: 1000000,
        pound: 453.59237,
        ounce: 28.349523125
    };

    const grams = value * toGram[fromUnit];

    // 从克转换为目标单位
    const fromGram = {
        gram: 1,
        kilogram: 0.001,
        milligram: 1000,
        ton: 0.000001,
        pound: 1 / 453.59237,
        ounce: 1 / 28.349523125
    };

    return grams * fromGram[toUnit];
};

export const convert = (value, fromUnit, toUnit, category) => {
    if (fromUnit === toUnit) return value;

    switch (category) {
        case 'temperature':
            return convertTemperature(value, fromUnit, toUnit);
        case 'length':
            return convertLength(value, fromUnit, toUnit);
        case 'time':
            return convertTime(value, fromUnit, toUnit);
        case 'weight':
            return convertWeight(value, fromUnit, toUnit);
        default:
            return value;
    }
};